import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  UseInterceptors,
} from '@nestjs/common';
import { DictService } from './dict.service';
import { DictPageRequest } from './dto/dict.request';
import { DictTypeForm } from './dto/dict.type.form';
import { DictForm } from './dto/dict.form';
import {
  AuthInterceptor,
  RequiredPermission,
} from '../../config/filter/auth.interceptor';

@Controller('/api/v1/dict')
export class DictController {
  constructor(private dictService: DictService) {}

  /**
   * 字典类型分页列表
   */
  @Get('/types/page')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async typesPage(@Query() query: DictPageRequest) {
    const result = await this.dictService.typesPage(query);
    return result;
  }

  /**
   * 字典下拉列表
   */
  @Get('/:typeCode/options')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async dictOptions(@Param('typeCode') typeCode: string) {
    const result = await this.dictService.dictOptions(typeCode);
    return result;
  }

  /**
   * 字典数据表单数据
   */
  @Get('/:id/form')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async dictForm(@Param('id') id: number) {
    const result = await this.dictService.getDictForm(id);
    return result;
  }
  /**
   * 字典类型表单数据
   */
  @Get('/types/:id/form')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async typesForm(@Param('id') id: number) {
    const result = await this.dictService.typesForm(id);
    return result;
  }
  /**
   * 字典分页列表
   */
  @Get('/page')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  async page(@Query() query: DictPageRequest) {
    const result = await this.dictService.page(query);
    return result;
  }

  /**
   * 新增字典类型
   */
  @Post('/types')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dict_type:add')
  async addType(@Body() form: DictTypeForm) {
    const result = await this.dictService.addDictType(form);
    return result;
  }

  /**
   * 新增字典
   */
  @Post('')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dict:add')
  async addDict(@Body() form: DictForm) {
    const result = await this.dictService.addDict(form);
    return result;
  }

  /**
   * 修改字典
   */
  @Put('/:id')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dict:edit')
  async updateDict(@Param('id') id: number, @Body() form: DictForm) {
    const result = await this.dictService.updateDict(id, form);
    return result;
  }

  /**
   * 删除字典
   */
  @Delete('/:ids')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dict:delete')
  async deleteDict(@Param() ids) {
    const result = await this.dictService.deleteDict(ids.ids);
    return result;
  }
  /**
   * 修改字典类型
   */
  @Put('/types/:id')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dict_type:edit')
  async updateDictType(@Param('id') id: number, @Body() form: DictTypeForm) {
    const result = await this.dictService.updateDictType(id, form);
    return result;
  }

  /**
   * 删除字典类型
   */
  @Delete('/types/:ids')
  @UseInterceptors(AuthInterceptor) // token验签拦截器
  @RequiredPermission('sys:dict_type:delete')
  async deleteDictType(@Param('ids') ids: string) {
    const result = await this.dictService.deleteDictType(ids);
    return result;
  }
}
